# -*- coding: UTF-8 -*-
import json
import traceback

import pymongo

from pymongo import MongoClient
from datetime import datetime
import matplotlib.pyplot as plt
import numpy as np
import time
import pandas as pd

desired_width = 320
pd.set_option('display.width', desired_width)
pd.set_option('display.max_columns', 20)

if __name__ == '__main__':
    try:
        db = MongoClient('mongodb://39.104.227.148:27018/')['exchange']
        cursor = db['okex_btc_usdt'].find({"_id": {"$gt": 1544140740000}}).sort([("_id", pymongo.ASCENDING)])
        df = pd.DataFrame(list(cursor))
        print(df)

        df['time'] = pd.to_datetime(df['_id'], unit="ms")
        print("===========================")
        print(df)
        new_df = df.get(key=['time', 'open', 'high', 'low', 'close']).copy()
        new_df['volume'] = 0
        print(new_df)
        new_df.to_csv('price201903.csv', index=False)
    except Exception as e:
        traceback.print_exc()
